//
//  VideoPlayView.m
//  WWXELuShang
//
//  Created by lanou on 15/12/21.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "VideoPlayView.h"
#import "PlayerView.h"
#define kPlayViewWidth self.bounds.size.width
#define kPlayViewHeight self.bounds.size.height


@implementation VideoPlayView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.playerView];
        [self addSubview:self.backButton];
        [self addSubview:self.stateButton];
        [self addSubview:self.videoProgress];
        [self addSubview:self.videoSlider];
        [self addSubview:self.nowTimeLabel];
        [self addSubview:self.timeLabel];
        [self addSubview:self.fullButton];
        self.controlHidden = NO;
    }
    return self;
}


- (PlayerView *)playerView {
    if (!_playerView ) {
        _playerView = [[PlayerView alloc] initWithFrame:CGRectMake(0, 0, kPlayViewWidth, kPlayViewWidth / 16 * 9)];
        _playerView.center = self.center;
        _playerView.backgroundColor = [UIColor grayColor];
    }
    return _playerView;
}


- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
        [_backButton setImage:[[UIImage imageNamed:@"topbar_icon_close_pressed@3x"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
        [_backButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _backButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _backButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        
    }
    return _backButton;
    
}

- (UIButton *)stateButton {
    if (!_stateButton) {
        _stateButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        [_stateButton setImage:[[UIImage imageNamed:@"download_begain@2x"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
        _stateButton.frame = CGRectMake(kWidth / 40, kPlayViewHeight / 2 + kPlayViewWidth * 9 / 16 / 2 - kWidth / 5, kWidth / 10, kWidth / 10);
    }
    return _stateButton;
    
}


- (UISlider *)videoSlider {
    if (!_videoSlider) {
        _videoSlider = [[UISlider alloc] initWithFrame:CGRectMake(kWidth / 40 + kWidth / 10, kPlayViewHeight / 2 + kPlayViewWidth * 9 / 16 / 2 - kWidth / 20, kPlayViewWidth - kWidth / 5 - kWidth / 40, kWidth / 20)];
        [_videoSlider setThumbImage:[UIImage imageNamed:@"whitePoint"] forState:(UIControlStateNormal)];
    }
    return _videoSlider;
}

- (UIProgressView *)videoProgress {
    if (!_videoProgress) {
        _videoProgress = [[UIProgressView alloc] initWithFrame:self.videoSlider.frame];
        _videoProgress.center = self.videoSlider.center;
    }
    return _videoProgress;
}

- (UILabel *)nowTimeLabel {
    if (!_nowTimeLabel) {
        _nowTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth / 40, self.videoSlider.frame.origin.y, kWidth / 10, kWidth / 20 )];
        _nowTimeLabel.text = @"00:00";
        _nowTimeLabel.textColor = [UIColor whiteColor];
        _nowTimeLabel.font = [UIFont systemFontOfSize:kWidth / 30];
    }
    return _nowTimeLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.videoSlider.frame.size.width + self.videoSlider.frame.origin.x, self.videoSlider.frame.origin.y, kWidth / 10,  kWidth / 20)];
        _timeLabel.text = @"00:00";
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.font = [UIFont systemFontOfSize:kWidth / 30];
    }
    return _timeLabel;
}

- (UIButton *)fullButton {
    if (!_fullButton) {
        _fullButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _fullButton.frame = CGRectMake(self.timeLabel.frame.size.width - kWidth / 10 + self.timeLabel.frame.origin.x, self.videoSlider.frame.origin.y - kWidth / 10, kWidth / 10, kWidth / 10);
        [_fullButton setImage:[[UIImage imageNamed:@"player_full@2x"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
    }
    return _fullButton;
}

- (void)changeDirection {
    
    if (kPlayViewWidth < kPlayViewHeight) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        self.transform = CGAffineTransformIdentity;
        self.transform = CGAffineTransformMakeRotation(M_PI_2);
        self.bounds = CGRectMake(0, 0, kHeight, kWidth);
        self.playerView.frame = CGRectMake(0, 0, kHeight, kWidth);
        [self reFrame];
        [_fullButton setImage:[[UIImage imageNamed:@"player_small@2x"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
        self.backButton.hidden = YES;
        [UIView commitAnimations];

    }else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0];
        self.transform = CGAffineTransformIdentity;
        self.transform = CGAffineTransformMakeRotation(0);
        self.bounds = CGRectMake(0, 0, kWidth, kHeight);
        [self reFrame];
        [_fullButton setImage:[[UIImage imageNamed:@"player_full@2x"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] forState:(UIControlStateNormal)];
        self.backButton.hidden = NO;
        [UIView commitAnimations];

    }
    
    

    
}

- (void)controlMakeHidden {
    self.controlHidden = !_controlHidden;
    self.stateButton.hidden = self.controlHidden;
    self.nowTimeLabel.hidden = self.controlHidden;
    self.timeLabel.hidden = self.controlHidden;
    self.videoSlider.hidden = self.controlHidden;
    self.videoProgress.hidden = self.controlHidden;
    self.fullButton.hidden = self.controlHidden;
    
}

- (void)reFrame {
    
    _playerView.frame = CGRectMake(0, 0, kPlayViewWidth, kPlayViewWidth / 16 * 9);
    _playerView.center = CGPointMake(kPlayViewWidth / 2, kPlayViewHeight / 2);
     _stateButton.frame = CGRectMake(kWidth / 40, kPlayViewHeight / 2 + kPlayViewWidth * 9 / 16 / 2 - kWidth / 5, kWidth / 10, kWidth / 10);
     _videoSlider.frame = CGRectMake(kWidth / 40 + kWidth / 10, kPlayViewHeight / 2 + kPlayViewWidth * 9 / 16 / 2 - kWidth / 20, kPlayViewWidth - kWidth / 5 - kWidth / 40, kWidth / 20);
    _videoProgress.frame = self.videoSlider.frame;
    _videoProgress.center = self.videoSlider.center;
    _nowTimeLabel.frame = CGRectMake(kWidth / 40, self.videoSlider.frame.origin.y, kWidth / 10, kWidth / 20 );
    _timeLabel.frame = CGRectMake(self.videoSlider.frame.size.width + self.videoSlider.frame.origin.x, self.videoSlider.frame.origin.y, kWidth / 10,  kWidth / 20);
     _fullButton.frame = CGRectMake(self.timeLabel.frame.size.width - kWidth / 10 + self.timeLabel.frame.origin.x, self.videoSlider.frame.origin.y - kWidth / 10, kWidth / 10, kWidth / 10);
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
